package com.moon.interview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moon.interview.exception.SysErrorCode;
import com.moon.interview.exception.ApplicationException;
import com.moon.interview.mapper.QuestionBankMapper;
import com.moon.interview.model.dto.question.QuestionQueryRequest;
import com.moon.interview.model.dto.questionBank.QuestionBankAddRequest;
import com.moon.interview.model.dto.questionBank.QuestionBankQueryRequest;
import com.moon.interview.model.dto.questionBank.QuestionBankUpdateRequest;
import com.moon.interview.model.entity.QuestionBank;
import com.moon.interview.model.entity.User;
import com.moon.interview.model.vo.QuestionBankVo;
import com.moon.interview.model.vo.QuestionSimpleVo;
import com.moon.interview.service.IQuestionBankService;
import com.moon.interview.service.IQuestionService;
import com.moon.interview.service.IUserService;
import com.moon.interview.utils.UserUtils;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionBankServiceImpl extends ServiceImpl<QuestionBankMapper, QuestionBank> implements IQuestionBankService {

    @Resource
    private IUserService userService;
    @Lazy
    @Resource
    private IQuestionService questionService;

    /**
     * 获取QuestionBankVo
     * @param questionBank
     * @return
     */
    public QuestionBankVo getQuestionBankVo(QuestionBank questionBank) {
        QuestionBankVo questionBankVo = new QuestionBankVo();
        BeanUtils.copyProperties(questionBank, questionBankVo);
        // 获取用户信息
        User user = userService.getById(questionBank.getUserId());
        questionBankVo.setUserName(user != null ? user.getUserName():"");
        return questionBankVo;
    }

    /**
     * 组装查询条件
     * @param request
     * @return
     */
    public LambdaQueryWrapper assembleCondition(QuestionBankQueryRequest request) {
        LambdaQueryWrapper<QuestionBank> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(request.getUserId() != null, QuestionBank::getUserId, request.getUserId()); // userId
        wrapper.like(StringUtils.isNotBlank(request.getTitle()), QuestionBank::getTitle, request.getTitle()); // 标题
        wrapper.like(StringUtils.isNotBlank(request.getDescription()), QuestionBank::getDescription, request.getDescription()); // 描述
        wrapper.last(request.getOrderSql()); // 排序

        return wrapper;
    }
    @Override
    public Page<QuestionBank> page(QuestionBankQueryRequest request) {
        // 分页查询
        Page<QuestionBank> page = this.page(new Page<>(request.getPageNum(), request.getPageSize()),assembleCondition(request));
        return page;
    }

    @Override
    public Page<QuestionBankVo> pageQuestionBankVo(QuestionBankQueryRequest request) {
        // 获取题库信息
        Page<QuestionBank> page = this.page(new Page<>(request.getPageNum(), request.getPageSize()),assembleCondition(request));
        Page<QuestionBankVo> pageVo = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
        // 组装vo
        List<QuestionBankVo> questionBankList = new ArrayList<>();
        for (QuestionBank questionBank : page.getRecords()) {
            QuestionBankVo questionBankVo = getQuestionBankVo(questionBank);
            questionBankList.add(questionBankVo);
        }
        pageVo.setRecords(questionBankList);
        return pageVo;
    }

    @Override
    public Page<QuestionBankVo> pageQuestionBankVoByUserId(QuestionBankQueryRequest queryRequest) {
        // 获取登录信息
        User user = UserUtils.getLoginUser();
        queryRequest.setUserId(user.getId());
        Page<QuestionBankVo> questionBankVoPage = pageQuestionBankVo(queryRequest);

        return questionBankVoPage;
    }

    @Override
    public boolean addQuestionBank(QuestionBankAddRequest addRequest) {
        // 获取登录信息
        User user = UserUtils.getLoginUser();
        // 转换类型
        QuestionBank questionBank = new QuestionBank();
        BeanUtils.copyProperties(addRequest, questionBank);
        // 设置userId
        questionBank.setUserId(user.getId());
        boolean flag = this.save(questionBank);
        if(!flag) {
            throw new ApplicationException(SysErrorCode.OPERATION_ERROR,"新增失败");
        }
        return true;
    }

    @Override
    public boolean updateQuestionBank(QuestionBankUpdateRequest updateRequest) {
        // 转换类型
        QuestionBank questionBank = new QuestionBank();
        BeanUtils.copyProperties(updateRequest, questionBank);
        boolean flag = this.updateById(questionBank);
        if(!flag) {
            throw new ApplicationException(SysErrorCode.OPERATION_ERROR,"更新失败");
        }
        return true;
    }

    @Override
    public boolean removeQuestionBank(Long id) {
        boolean flag = this.removeById(id);
        if(!flag) {
            throw new ApplicationException(SysErrorCode.OPERATION_ERROR,"删除失败");
        }
        return true;
    }

    @Override
    public QuestionBankVo getBankDetailWithSimpleQuestionById(Long id) {
        QuestionBank questionBank = this.getById(id);
        if (questionBank == null) {
            throw new ApplicationException(SysErrorCode.OPERATION_ERROR,"题库不存在");
        }
        QuestionBankVo questionBankVo = getQuestionBankVo(questionBank);
        // 获取题目详情
        QuestionQueryRequest queryRequest = new QuestionQueryRequest();
        queryRequest.setBankId(id);
        queryRequest.setPageSize(500);
        Page<QuestionSimpleVo> questionSimpleVoList = questionService.pageQuestionSimpleVo(queryRequest);
        questionBankVo.setQuestion(questionSimpleVoList);
        return questionBankVo;
    }


}
